﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Skywave.DataStructure
{
    public enum BinaryTreeNavigationMethods
    {
        LRD,
        LDR,
        DLR
    }

    public class BinaryTree
    {
        public BinaryTreeNode Root
        {
            get
            {
                return mRoot;
            }
            set
            {
                mRoot = value;
            }
        }
        private BinaryTreeNode mRoot;

        public Queue<BinaryTreeNode> GetNavigation(BinaryTreeNavigationMethods method)
        {
            if (mRoot == null) throw new ArgumentException("No node exist in tree!");
            Queue<BinaryTreeNode> r = new Queue<BinaryTreeNode>();
            navMethod = method;
            makeNavigation(r, Root);
            return r;
        }

        private BinaryTreeNavigationMethods navMethod;
        private void makeNavigation(Queue<BinaryTreeNode> r, BinaryTreeNode navNode)
        {
            switch (navMethod)
            {
                case BinaryTreeNavigationMethods.LRD:
                    if (navNode.Left != null) makeNavigation(r, navNode.Left);
                    if (navNode.Right != null) makeNavigation(r, navNode.Right);
                    r.Enqueue(navNode);
                    break;
                case BinaryTreeNavigationMethods.LDR:
                    if (navNode.Left != null) makeNavigation(r,navNode.Left);
                    r.Enqueue(navNode);
                    if (navNode.Right != null) makeNavigation(r, navNode.Right);
                    break;
                case BinaryTreeNavigationMethods.DLR:
                default:
                    r.Enqueue(navNode);
                    if (navNode.Left != null) makeNavigation(r, navNode.Left);
                    if (navNode.Right != null) makeNavigation(r, navNode.Right);
                    break;
            }
        }
    }
}
